package com.yjc.drygoods.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.yjc.common.constant.EsConstant;
import com.yjc.common.to.SkuEsModel;
import com.yjc.drygoods.product.config.DryGoodsElasticSearchConfig;
import com.yjc.drygoods.product.entity.SkuInfoEntity;
import com.yjc.drygoods.product.entity.SpuCommentEntity;
import com.yjc.drygoods.product.service.SkuInfoService;
import com.yjc.drygoods.product.service.SpuCommentService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.activation.FileDataSource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 推荐服务
 *
 * @author liu yapeng
 * @version 1.0
 * @copyright (c) 2019 LuoYang TuLian Co'Ltd Inc. All rights reserved.
 * @date 2020/7/31$ 16:18$
 * @since JDK1.8
 */
@Slf4j
@Service
public class Recommend {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SpuCommentService spuCommentService;
    @Autowired
    CoreMath coreMath;


    /**
     * 方法描述: 猜你喜欢
     *
     * @param
     * @throws
     * @Return {@link List<SkuInfoEntity>}
     * @author tarzan
     * @date 2020年07月31日 17:28:06
     */
    public List<SkuInfoEntity> guessUserLike(String username, List<Long> skuIds) throws IOException {

        //加载用户与商品关系的数据(用户id和已评论商品id，以及对其的评分)
        List<SpuCommentEntity> data = spuCommentService.getAllSpuCommentInfo();
        log.info("data:{}", data);
        if (data != null && data.size() > 0) {
            List<Long> recommendations = coreMath.recommend(username, data);
            log.info("recommendations:{}", recommendations);
            if (recommendations != null && recommendations.size() > 0) {
                //加载所有商品的信息(使用skuId作为索引)
                log.info("skuIds：{}",skuIds);
                List<SkuInfoEntity> itemList = skuInfoService.getAllSkuInfo().stream().filter(e -> skuIds.contains(e.getSkuId()) && recommendations.contains(e.getSkuId())).collect(Collectors.toList());
                log.info("itemList:{}", itemList);
                return itemList;
            }
        }
        return null;
    }


}
